import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class Practice1 {
    public static void main(String[] args) throws InterruptedException {
        //Test1();
        //Test2();
        //Test3();
        //Test4();
        //Test5();
        Test6();
    }

    private static void Test1() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        //使用css定位
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("selenium");
        //使用Xpath定位
         webDriver.findElement(By.xpath("//*[@id=\"su\"]")).click();
//         //强制等待
//         Thread.sleep(3000);
        //隐式等待
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        List<WebElement> elements = webDriver.findElements(By.cssSelector("#content_left > div:nth-child(4) > div > div > div > div > div:nth-child(1) > div > div > h3 > div > a > font"));
        System.out.println(elements);
        for (int i = 0; i < elements.size(); i++) {
            if(elements.get(i).getText().equals("selenium")) {
                System.out.println("测试通过");
                break;
            }else {
                System.out.println("测试不通过");
            }
        }
        webDriver.quit();
    }

    private static void Test2() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        //使用css定位
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("selenium");
        //使用Xpath定位
        webDriver.findElement(By.xpath("//*[@id=\"su\"]")).click();
        //显示等待
        WebDriverWait wait = new WebDriverWait(webDriver, 3);
        //WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#content_left > div:nth-child(4) > div > div > div > div > div:nth-child(1) > div > div > h3 > div > a > font")));
        Boolean is_true = wait.until(ExpectedConditions.textToBe(By.cssSelector("#content_left > div:nth-child(4) > div > div > div > div > div:nth-child(1) > div > div > h3 > div > a > font"), "selenium"));
        if(is_true) {
            System.out.println("测试通过");
        }else {
            System.out.println("测试不通过");
        }
        webDriver.quit();
    }

    //窗口的切换
    private static void Test3() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        //使用css定位
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        Set<String> windowHandles = webDriver.getWindowHandles();
        String target = "";
        for (String temp : windowHandles) {
            target = temp;
        }
        webDriver.switchTo().window(target);
        Thread.sleep(3000);
        webDriver.findElement(By.xpath("//*[@id=\"ww\"]")).sendKeys("软件测试");
        webDriver.findElement(By.cssSelector("#s_btn_wr")).click();
        List<WebElement> elements = webDriver.findElements(By.cssSelector("#\\32  > div > h3 > a > em"));
        int flag = 0;
        for (int i = 0; i < elements.size(); i++) {
            if(elements.get(i).getText().equals("软件测试")) {
                flag = 1;
                break;
            }
        }
        if(flag == 1) {
            System.out.println("测试通过");
        }else {
            System.out.println("测试不通过");
        }
        webDriver.quit();
    }

    //弹窗关闭和确认
    private static void Test4() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/softwaretest/test04.html?_ijt=defkcrh3pvflsc8o1n6g57103k&_ij_reload=RELOAD_ON_SAVE");
        //使用css定位
        webDriver.findElement(By.cssSelector("body > button")).click();
        Thread.sleep(3000);
        webDriver.switchTo().alert().dismiss();
        webDriver.findElement(By.cssSelector("body > button")).click();
        String name = "凌亮";
        webDriver.switchTo().alert().sendKeys(name);
        Thread.sleep(3000);
        webDriver.switchTo().alert().accept();
        WebElement element = webDriver.findElement(By.cssSelector("body > div:nth-child(5)"));
        if(element.getText().equals(name)) {
            System.out.println("测试通过");
        }else {
            System.out.println("测试不通过");
        }
        webDriver.quit();
    }

    //选择框的处理
    private static void Test5() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/softwaretest/test01.html?_ijt=6vpic5otkrhk6smjcu3bhjippn&_ij_reload=RELOAD_ON_SAVE");
        //使用css定位
        List<WebElement> webElements = webDriver.findElements(By.cssSelector("input"));
        for (int i = 0; i < webElements.size(); i++) {
            if(webElements.get(i).getAttribute("type").equals("checkbox")) {
                webElements.get(i).click();
            }
        }
    }

    //文件上传操作
    private static void Test6() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/softwaretest/test05.html?_ijt=7tdn2eoep0pj7b72b5eu84rn1v&_ij_reload=RELOAD_ON_SAVE");
        //使用css定位
        webDriver.findElement(By.cssSelector("body > input[type=file]")).sendKeys("D:\\Java108\\Java108\\Selenium01\\css和xpath学习手册.txt");
    }
}
